package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;


    @RequestMapping("/findParentId")
    public Result findParentId(){
        List<Menu> list = menuService.findParentId();
        return new Result(true,"成功",list);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try{
            menuService.add(menu);//发送请求
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findPage(queryPageBean);
    }


    //根据id查询检查项
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Menu menu = menuService.findById(id);//发送请求
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try{
            menuService.edit(menu);//发送请求
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    //根据id删除检查项
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            menuService.delete(id);//发送请求
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL );
        }
    }

    @RequestMapping("/findMenuByUserName")
    public Result findMenuByUserName(String username){
        List<Menu> list = menuService.findMenuByUserName(username);
        return new Result(true,"成功",list);
    }

    @RequestMapping("/findChildrensByParentId")
    public Result findChildrensByParentId(int id){
        List<Menu> list = menuService.findChildrensByParentId(id);
        return new Result(true,"成功",list);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Menu> list = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/getSonMenuByID")
    public Result getSonMenuByID(String label){
        List<Menu> menuList= menuService.getSonMenuByID(label);
        return  new Result(true,"success",menuList)  ;
    }

}
